<?php
include '401.php';
include '../includes/common.php';
include '../ss-config.php';
include '../includes/ss-db.php';
include 'includes/Constant.php';
include 'includes/functions.php';


//添加新积分
if (isset($_GET['add'])) 
{
	$vip_number = (isset($_POST['vip_number'])) ? trim($_POST['vip_number']) : '';
	$vip_integral = (isset($_POST['vip_integral'])) ? trim($_POST['vip_integral']) : '';
	$vip_user_address = (isset($_POST['vip_user_address'])) ? trim($_POST['vip_user_address']) : '';
	$submit_date = date('Y-m-d H:i:s');
	
	
	//如必填项为空则提示
	if ($vip_number == '' || $vip_integral == '') 
	 {
	     echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; 
	     echo '<script>alert("VIP卡号或积分数不能为空!");history.back(-1);</script>';
	 }
	  else 
	 {
		$query = sprintf('INSERT INTO %sintegral (vip_number, vip_integral, vip_user_address, SUBMIT_DATE) ' .
							'VALUES ("%s", "%s", "%s", "%s")',
							
							DB_TBL_PREFIX,
							mysql_real_escape_string($vip_number, $GLOBALS['DB']),
							mysql_real_escape_string($vip_integral, $GLOBALS['DB']),
							mysql_real_escape_string($vip_user_address, $GLOBALS['DB']),
							mysql_real_escape_string($submit_date, $GLOBALS['DB'])
							);
							
		mysql_query("set names 'utf8'");
		mysql_query($query, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
		
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; 
		echo '<script>alert("添加积分成功!");location.href="integral_manage.php?list";</script>';
	 }
}

//编辑积分
if (isset($_GET['edit'])) 
{
	$item_id = $_POST['item_id']; // 接收要编辑的数据id
	$page = $_POST['page']; // 接收页码以便在编辑完成后跳转回原页面
	
	//定义编辑完成后跳转URL中的参数
	$redi_pars = '&page=' . $page;
	
	//如果用户执行搜索后进行编辑操作 则接收搜索关键词参数 并在编辑完成后跳转回该搜索结果
	if (isset($_POST['keywrd']))
	{
		$redi_pars .= '&keyword=' . $_POST['keyword'];
	}
	
	$vip_number = (isset($_POST['vip_number'])) ? trim($_POST['vip_number']) : '';
	$vip_integral = (isset($_POST['vip_integral'])) ? trim($_POST['vip_integral']) : '';
	$vip_user_address = (isset($_POST['vip_user_address'])) ? trim($_POST['vip_user_address']) : '';
	$submit_date = date('Y-m-d H:i:s');
	
	
	//如必填项为空则提示
	if ($vip_number == '' || $vip_integral == '') 
	 {
	     echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; 
	     echo '<script>alert("VIP卡号或积分数不能为空!");history.back(-1);</script>';
	 }
	  else 
	 {
		$query = sprintf('UPDATE %sintegral SET ' .
						'vip_number = "%s",
						 vip_integral = "%s",
						 vip_user_address = "%s",
						 SUBMIT_DATE = "%s"
						 WHERE
						 ID = %d',
						 
						 DB_TBL_PREFIX,
						 mysql_real_escape_string($vip_number, $GLOBALS['DB']),
						 mysql_real_escape_string($vip_integral, $GLOBALS['DB']),
						 mysql_real_escape_string($vip_user_address, $GLOBALS['DB']),
						 mysql_real_escape_string($submit_date, $GLOBALS['DB']),
						 $item_id);
							
		mysql_query("set names 'utf8'");
		mysql_query($query, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
		
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; 
		echo '<script>alert("编辑积分成功!");location.href="integral_manage.php?list' . $redi_pars . '";</script>';
	 }
}

//删除数据
if (isset($_GET['del'])) 
{
	$item_id = $_GET['id']; // 接收要删除的数据id
	$page = $_GET['page']; // 接收页码以便在编辑完成后跳转回原页面
	
	//定义编辑完成后跳转URL中的参数
	$redi_pars = '&page=' . $page;
	
	//如果用户执行搜索后进行编辑操作 则接收搜索关键词参数 并在编辑完成后跳转回该搜索结果
	if (isset($_POST['keywrd']))
	{
		$redi_pars .= '&keyword=' . $_POST['keyword'];
	}
	
	//删除记录
	$query = sprintf('DELETE FROM %sintegral WHERE ID = %d', DB_TBL_PREFIX, $item_id);
	mysql_query("set names 'utf8'");
	
	mysql_query("set names 'utf8'");
	mysql_query($query, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
	
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; 
	echo '<script>alert("删除成功!");location.href="integral_manage.php?list' . $redi_pars . '";</script>';
}

//批量删除数据
if (isset($_GET['delitems'])) 
{
	$del_ids = $_GET['item_ids']; //接收要删除数据的id集合  格式如 2,5,6,8
	
	//批量删除记录
	$query = sprintf('DELETE FROM %sintegral WHERE ID IN  (%s)', DB_TBL_PREFIX, $del_ids);
	
	mysql_query("set names 'utf8'");
	mysql_query($query, $GLOBALS['DB']) or die(mysql_error($GLOBALS['DB']));
	
	echo '<script>location.href="integral_manage.php?list";</script>';
}
?>